
//记录查询出的所有用户信息
var replyAutoobj=new Array();

//记录点击当前行的用户ID 修改用户信息使用
var replyAutoGridnwxMessageId;

function replyAutoGridColumnsRenderRolelink(nwxMessageId,sname){
	replyAutoGridnwxMessageId=nwxMessageId;
	showreplyAutoRoleForm(nwxMessageId,sname);
}
function replyAutoGridColumnsRenderdellink(nwxMessageId,sname){
	
	Ext.MessageBox.confirm('信息', '是否确认删除消息： '+sname+'?', function(btn){
		
		if(btn=='yes'){
			Ext.Ajax.request({
				url:'delReplyNotice.do',
				method:'POST',
				params:{
					ids:nwxMessageId
				},
				success:function(response){
					if(response.responseText=='1'){
						Ext.MessageBox.alert('信息','删除成功！');
						replyAutoGridStore.reload();
					}else{
						Ext.MessageBox.alert('ERROR','删除失败！');
					}
				},
				failure:function(){
					Ext.Msg.alert('ERROR','错误');
				}
			});
		}
	});
}

function replyAutoGridColumnsRenderInfolink(nwxMessageId){
	replyAutoGridnwxMessageId=nwxMessageId;
	if(replyAutoGridnwxMessageId==null)return ;
	showreplyAutoAddForm(2,replyAutoobj[nwxMessageId]);
}

var replyAutoAddWin;
var replyAutoTitle=new Ext.form.TextField({
		labelAlign:'right',
		width:390,
		fieldLabel: '消息标题'
});
var replyAutosDescription=new Ext.form.TextArea({
		labelAlign:'right',
		width:390,
		height:100,
		fieldLabel: '消息描叙'
});
var replyAutosPicUrl=new Ext.form.FileUploadField({
		id: 'form-file',
		width:188,
		buttonText: '浏览...',
        emptyText: '选择一张图片',
        fieldLabel: '上传图片',
        name: 'txtpath',
        buttonCfg: {
        	width: 30,
            iconCls: 'upload-icon'
        }
});
var replyAutosUrl=new Ext.form.ComboBox({
		fieldLabel: '点击跳转链接',
		width:155,
        store: new Ext.data.JsonStore({
        	autoLoad:true,
            url:'selectUserUrl.do',
            fields:['sName','nWxUserUrlId']
        }),
        mode:'local',
	    valueField:'nWxUserUrlId',
	    displayField:'sName',
        emptyText:'全部',
        triggerAction:'all',
        selectOnFocus:true
	});
var replyAutonisValid=new Ext.form.Checkbox({
		fieldLabel:'是否有效',
		labelAlign:'right',
		checked:true
});

var replyAutodelPicBtn=new Ext.Button({
		text:'取消上传',
		handler:function(){
			replyAutosPicUrl.reset();
		}
});
var replyAutonOrder=new Ext.form.NumberField({
		labelAlign:'right',
		width:155,
		value:0,
		fieldLabel: '排序号'
});

var replyAutoExplain=new Ext.form.Label({
	labelAlign:'right',
	text:'说明：排序号按数字从小到大排列 多条图文消息信息中，默认第一条消息显示为大图,注意，消息数不能超过10条（上传图片仅支持JPG、PNG格式，较好的效果为大图360*200，小图200*200）'
});

var replyAutoImage = new Ext.BoxComponent({
//    id: 'imgPosition',  
    //hidden:true,  
    xtype: 'box',  
    width: 120,  
    height: 80,  
    style: '',  
    autoEl: {
        tag: 'img',
        id:"imgPosition",
	 	name:"imgPosition",
        src: '' //指定url路径  
    }  
});

//tinfo参数值1 :添加 2:修改
function showreplyAutoAddForm(tinfo,replyAutoJson) {
	
	if(!replyAutoAddWin){
		replyAutoAddform = new Ext.FormPanel({
			fileUpload: true,
		    border: false,
		    labelAlign:'right',
		    frame:true,
		    items: [
		    	{columnWidth:1.0,layout:'column',labelAlign:'right',items:[
		    		{columnWidth:.6,layout:'form',labelAlign:'right',items:[replyAutosPicUrl]},replyAutodelPicBtn
		    	]},
                {layout:'column',items:[
                	 {columnWidth:.65,layout:'form',items:[
                	 replyAutosUrl,replyAutonOrder,replyAutonisValid
	                ]},
	                {columnWidth:.35,layout:'form',items:[
	                	replyAutoImage
	                ]}
                ]},
            	replyAutoTitle,replyAutosDescription,replyAutoExplain
		    ],
		    buttons: [{
		        text: tinfo,
		        handler: function() {
		        	if(this.text=='添 加'){
		        		 replyAutoAddform.getForm().submit({
		                    url: 'saveReplyAuto.do',
		                    method:'POST',
		                    waitMsg: 'Uploading your data...',
		                    params : {
		                    	sTitle:replyAutoTitle.getValue(),
								sDescription:replyAutosDescription.getValue(),
			                   	nWxUserUrlId:replyAutosUrl.getValue(),
			                   	nOrder:replyAutonOrder.getValue(),
			                   	nisValid:replyAutonisValid.checked?1:2
		                    },
		                    success: function(fm, o){
		                    	if(o.result.success=='1'){
		                    		Ext.MessageBox.alert('信息!', '添加成功!');
			                        replyAutoAddform.getForm().reset();
			                        replyAutoAddWin.hide();
			                        replyAutoGridStore.reload();
		                    	}else if(o.result.success=='3'){
		                    		Ext.MessageBox.alert('Error!', '图片格式不正确（仅支持JPG、PNG）!');
		                    	}else{
		                    		Ext.MessageBox.alert('Error!', '添加失败!');
		                    	}
		                    },
							failure:function(form, action) {
								Ext.Msg.alert('错误', '添加失败！');
								return ;
							}
		                });
		        	}else if(this.text=='修 改'){
		        		replyAutoAddform.getForm().submit({
		                    url: 'updateReplyNotice.do',
		                    method:'POST',
		                    waitMsg: 'Uploading your data...',
		                    params : {
		                    	sTitle:replyAutoTitle.getValue(),
								sDescription:replyAutosDescription.getValue(),
			                   	nWxUserUrlId:replyAutosUrl.getValue(),
			                   	nOrder:replyAutonOrder.getValue(),
			                   	nisValid:replyAutonisValid.checked?1:2,
			                   	spicUrl:replyAutosPicUrl.getValue(),
			                   	nwxMessageId:replyAutoGridnwxMessageId
		                    },
		                    success: function(fm, o){
		                    	if(o.result.success=='1'){
		                    		Ext.MessageBox.alert('信息!', '修改成功!');
			                        replyAutoAddform.getForm().reset();
			                        replyAutoAddWin.hide();
			                        replyAutoGridStore.reload();
		                    	}else if(o.result.success=='3'){
		                    		Ext.MessageBox.alert('Error!', '图片格式不正确（仅支持JPG、PNG）!');
		                    	}else{
		                    		Ext.MessageBox.alert('Error!', '修改失败!');
		                    	}
		                    },
							failure:function(form, action) {
								Ext.Msg.alert('错误', '修改失败！');
								return ;
							}
		                });
		        	}
		        }
		    },{
		        text: '返 回',
		        handler: function() {
		        	replyAutoAddform.getForm().reset();
		        	replyAutoAddWin.hide();
		        }
		    }]
		});
		
		replyAutoAddWin =new Ext.Window({
		    title: '回复信息',
		    closeAction: 'hide',
		    width: 600,
		    height: 390,
		    layout: 'fit',
	        resizable: true,
	        modal: true,
	        items: replyAutoAddform,
	        listeners :{
	        	close:function(){
	        		replyAutoAddform.getForm().reset();
	        	}
	    	}
		});
	}
    replyAutoAddWin.show();
    
    if(tinfo==2){
    	replyAutoAddWin.setTitle('信息：'+replyAutoJson.stitle);
    	replyAutoAddform.buttons[0].setText('修 改');
    	
    	if(replyAutoJson.norder!=null)
    		replyAutonOrder.setValue(replyAutoJson.norder);
    	if(replyAutoJson.nisValid!=null)
    		replyAutonisValid.setValue(replyAutoJson.nisValid==1?true:false);
    	if(replyAutoJson.stitle!=null)
    		replyAutoTitle.setValue(replyAutoJson.stitle);
    	if(replyAutoJson.sdescription!=null)
    		replyAutosDescription.setValue(replyAutoJson.sdescription);
    	if(replyAutoJson.nwxUserUrlId!=null)
    		replyAutosUrl.setValue(replyAutoJson.nwxUserUrlId);
    	if(replyAutoJson.spicUrl!=null){
    		replyAutosPicUrl.setValue('1');
    		Ext.getDom('imgPosition').src=replyAutoJson.spicUrl==''?'#':replyAutoJson.spicUrl;
    	}
	}else{
		replyAutoAddform.buttons[0].setText('添 加');
		replyAutoAddform.getForm().reset();
		Ext.getDom('imgPosition').src='#';
	}
}

Ext.onReady(function(){
	replyAutoiswriteidb= Ext.get('replyAutoiswriteid').getValue()==1?true:false;
	
	replyAutoGridStore = new Ext.data.Store({
        autoLoad: true,
        proxy : new Ext.data.HttpProxy({
            url: 'searchReplyAuto.do'
        }),
        baseParams:{
//        	'sName':replyAutoNameSearchField.getValue(),
        },
        reader:new Ext.data.JsonReader({
             totalProperty: 'totalSize',
             root:'list'
        },[
        	{name: 'norder',mapping:'norder', type: 'int'},
            {name: 'nuserId',mapping:'nuserId', type: 'int'},
        	{name: 'nisValid',mapping:'nisValid',   type: 'int'},
        	{name: 'nwxMessageId',mapping:'nwxMessageId', type: 'int'},
        	{name: 'stitle',mapping:'stitle', type: 'string'},
        	{name: 'sdescription',mapping:'sdescription', type: 'string'},
        	{name: 'spicUrl',mapping:'spicUrl', type: 'string'},
        	{name: 'nwxUserUrlId',mapping:'nwxUserUrlId', type: 'int'},
        	{name: 'dcreateTime',mapping:'dcreateTime', type: 'date',
        		convert : function(v) {
        			return new Date(v);
        		}
        	}
        ])
    });
    
    replyAutoAddBtn =new Ext.Button({
		text:'添加消息',
		hidden:replyAutoiswriteidb,
		handler:function(){
			showreplyAutoAddForm(1,'');
		}
	});
    
    var replyAutoGridBar=new Ext.PagingToolbar({
		store: replyAutoGridStore,
		displayInfo: true,
		pageSize:20,
		displayMsg : '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
		emptyMsg: "没有记录可以显示",
		beforePageText:'当前页码',
		afterPageText:'共{0}页', 
		firstText:'第一页', 
		prevText:'上一页', 
		nextText:'下一页', 
		lastText:'最后一页', 
		refreshText:'刷新当前页',
		items:['-', replyAutoAddBtn]
     });
     
    replyAutoGridColumnsRenderInfo=function(value, cellmeta, record, rowIndex, columnIndex, store){
    	replyAutoobj[record.data.nwxMessageId]=record.json;
    	var linkhtml='';
    	if(replyAutoiswriteidb==false){
	    	linkhtml='<a style="margin-right:20px" onclick="replyAutoGridColumnsRenderInfolink('+record.data.nwxMessageId+')" href="javascript:void(0)">详细 </a>';
	    	linkhtml+='<a onclick="replyAutoGridColumnsRenderdellink('+record.data.nwxMessageId+',\''+record.data.stitle+'\')" href="javascript:void(0)"> 删除</a>';
    	}
    	return linkhtml;
	};
	
	replyAutoGrid = new Ext.grid.GridPanel({
        store: replyAutoGridStore,
        region:'center',
		disableSelection: true,
		border:false,
		frame:true,
		stripeRows:true,
		loadMask :{msg:"数据加载中，请稍等..."},
		layout:'fit',
		viewConfig:{
			forceFit: true,
			scrollOffset:0
		},
        cm:new Ext.grid.ColumnModel([
        	new Ext.grid.RowNumberer(),
            {header: "排序号", width:30, dataIndex: 'norder', sortable: true},
            {header: "标题", flex: 1, dataIndex: 'stitle', sortable: true},
            {header: "描叙",flex: 1, dataIndex: 'sdescription', sortable: true},
            {header: "是否有效", width:50,sortable: true,dataIndex: 'nisValid',
            	renderer:function(value){
            		if (value == 1) {
			            return '有效';
			        }else{
			        	return '无效';
			        }
            	}
            },
            {header: "创建时间",flex: 1, dataIndex: 'dcreateTime', sortable: true,renderer : Ext.util.Format.dateRenderer('Y-m-d H:i:s')},
            {header: "操作", width:150,align:'center',sortable: true,renderer:replyAutoGridColumnsRenderInfo}
        ]),
        bbar: replyAutoGridBar
    });
    
	 var mainPanel = new Ext.Viewport({
	 	layout:'border',
		renderTo:'replyAutoDIV',
		id:'replyAutoMainPanel',
		items:[replyAutoGrid],
		listeners: {
	        'resize': function(viewport,adjWidth,adjHeight,rawWidth,rawHeight)  {
	          	Ext.getCmp("replyAutoMainPanel").setHeight((document.body.clientHeight - 290) * 0.67);  
			}
    	}
	});
})